package evolution;

import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import evolution.algorithm.CoevolutionAlgorithm;
import evolution.data.CoevolutionAlgorithmFactory;
import evolution.gui.CoevolutionGUI;
import evolution.individual.Individual;

public class Launcher {

	/**
	 * @param args
	 *            (problem, dimensions, populations count, populations size,
	 *            epoch length, max time limit, epsilon, mutation type, mutation
	 *            probability, crossover type, crossover probability, selection
	 *            type, migration type, migration size)
	 */
	public static void main(String[] args) {
		if (args == null || args.length == 0) {
			System.out.println("No CLI arguments: running GUI mode.");

			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					CoevolutionGUI gui = CoevolutionGUI.getInstance();
					gui.pack();
					gui.setVisible(true);
					gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				}
			});
		} else {
			if (args.length != 13) {
				System.out.println("Parameters list:");
				System.out
						.println("problem, dimensions, populations count, populations size, epoch length, iterations, mutation type, mutation  probability, crossover type, crossover probability, selection type, migration type, migration size");
			}
			executeCLI(args);
		}
	}

	private static void executeCLI(String[] args) {
		CoevolutionAlgorithm algorithm = new CoevolutionAlgorithmFactory()
				.create(args);
		algorithm.run();

		Individual bestIndividual = algorithm.getBestIndividual();
		System.out.println(Arrays.toString(bestIndividual.getChromosome()));
		System.out.println(bestIndividual.getFitnessFunction()
				.convertToRealValue(bestIndividual.getFitness()));
	}
}
